package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxw.nonghe.entity.FarmerPaymentDetail;
import com.lxw.nonghe.mapper.FarmerPaymentDetailMapper;
import com.lxw.nonghe.service.FarmerPaymentDetailService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 参合人员缴费明细表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-05-14
 */
@Service
public class FarmerPaymentDetailServiceImpl
        extends ServiceImpl<FarmerPaymentDetailMapper, FarmerPaymentDetail>
        implements FarmerPaymentDetailService {

    @Override
    public Page<FarmerPaymentDetail> getPaymentDetailPage(
            int current, int size,
            String farmerName, String idCard, Byte status) {

        QueryWrapper<FarmerPaymentDetail> wrapper = new QueryWrapper<>();

        // 农民姓名模糊查询
        if (StringUtils.isNotBlank(farmerName)) {
            wrapper.like("farmer_name", farmerName.trim());
        }

        // 身份证号精确查询
        if (StringUtils.isNotBlank(idCard)) {
            wrapper.eq("id_card", idCard.trim());
        }

        // 缴费状态精确查询
        if (status != null) {
            wrapper.eq("status", status);
        }

        // 排序，最新缴费的在前面
        wrapper.orderByDesc("payment_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
    // 条件查询实现
    @Override
    public Page<FarmerPaymentDetail> queryPaymentDetails(
            FarmerPaymentDetail queryParam,
            int current, int size) {

        QueryWrapper<FarmerPaymentDetail> wrapper = new QueryWrapper<>();

        // 农民ID精确查询
        if (queryParam.getFarmerId() != null) {
            wrapper.eq("farmer_id", queryParam.getFarmerId());
        }

        // 农民姓名模糊查询
        if (StringUtils.isNotBlank(queryParam.getFarmerName())) {
            wrapper.like("farmer_name", queryParam.getFarmerName().trim());
        }

        // 身份证号精确查询
        if (StringUtils.isNotBlank(queryParam.getIdCard())) {
            wrapper.eq("id_card", queryParam.getIdCard().trim());
        }

        // 缴费金额范围查询 - 大于等于
        if (queryParam.getAmount() != null) {
            wrapper.ge("amount", queryParam.getAmount());
        }

        // 缴费状态精确查询
        if (queryParam.getStatus() != null) {
            wrapper.eq("status", queryParam.getStatus());
        }

        // 缴费时间范围查询 - 大于等于
        if (queryParam.getPaymentTime() != null) {
            wrapper.ge("payment_time", queryParam.getPaymentTime());
        }

        // 排序，最新缴费的在前面
        wrapper.orderByDesc("payment_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }
}
